﻿using HMS.DataContract.Request;
using HMS.DataContract.Response;
using HMS.Web.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using HMS.DataContact.Response;

namespace HMS.Web.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        [Authorize]
        public ActionResult Index()
        {
            //var client = InitializeSchedulerClient();
            //var report = client.LocationBasedPatientCount(DateTime.Now.AddDays(-60), DateTime.Now.AddDays(30));
            //return View(report);
            return View();
        }
        [Authorize]
        public JsonResult LocationBasedReport(string startDate, string endDate, int locId, int docId)
        {
            var client = InitializeSchedulerClient();
            DateTime startDateValue = Convert.ToDateTime(startDate);
            DateTime endDateValue = Convert.ToDateTime(endDate);
            UserInfo userInfo = (UserInfo)Session["userInfo"] != null ? (UserInfo)Session["userInfo"] : null;
            var report = new List<Lookup>();
            if (userInfo != null)
            {
                try
                {
                    var patientLocationReportResponseList = client.LocationBasedPatientCount(startDateValue, endDateValue, userInfo.ClinicId, locId, docId);
                    dynamic result = new System.Dynamic.ExpandoObject();
                    result.chart = patientLocationReportResponseList.GroupBy(x => x.Patient_Location_Name).Select(y => new PatientLocationReportResponse() { Patient_Location_Name = y.Key, PatientCount = y.Count() }).ToList(); ;
                    result.list = patientLocationReportResponseList;
                    return Json(result);
                }
                catch (Exception ex)
                {
                    return Json(report);
                }
            }
            return Json(report);
        }
        [Authorize]
        public JsonResult GetFinanceReport(string startDate, string endDate, int locId, int docId)
        {
            var client = InitializeSchedulerClient();
            DateTime startDateValue = Convert.ToDateTime(startDate);
            DateTime endDateValue = Convert.ToDateTime(endDate);
            UserInfo userInfo = (UserInfo)Session["userInfo"] != null ? (UserInfo)Session["userInfo"] : null;
            List<FinanceReportResponse> financeReport = new List<FinanceReportResponse>();
            if (userInfo != null)
            {
                try
                {
                    financeReport = client.GetFinanceReport(startDateValue, endDateValue, userInfo.ClinicId, locId, docId).ToList<FinanceReportResponse>();
                    dynamic result = new System.Dynamic.ExpandoObject(); 
                    result.chartData = financeReport
                    .GroupBy(l => l.Appt_Date)
                    .Select(
                        csLine => new
                        {
                            Date = csLine.First().Appt_Date.ToString("dd/MMM/yyyy"),
                            AmtPaid = csLine.Sum(c => c.Amt_Paid)
                        }).ToList();
                    result.listData = financeReport;
                    return Json(result);
                }
                catch (Exception ex)
                {
                    return Json(financeReport);
                }
            }

            return Json(financeReport);
        }

        /// <summary>
        /// Method to Initialize Scheduler Client
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public SchedulerClient InitializeSchedulerClient()
        {

            var binding = new BasicHttpBinding();
            binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 60000000);
            binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, 10000000);
            binding.MaxReceivedMessageSize = 10000000;
            binding.ReaderQuotas.MaxStringContentLength = 10000000;
            binding.ReaderQuotas.MaxDepth = 10000000;
            binding.ReaderQuotas.MaxArrayLength = 10000000;
            var endpointPath = ConfigurationManager.AppSettings["SchedulerEndpointPath"];
            var endpoint = new EndpointAddress(endpointPath);
            var client = new SchedulerClient(binding, endpoint);
            return client;
        }
    }
}
